import { alova } from "./index";

export interface ILoginParams {
  username: string;
  password: string;
}

export interface ILoginResult {
  token: string;
  user: object;
  success: boolean;
  message?: string;
}

export interface ICreateUserParams {
  username: string;
  password: string;
  roleId: number;
}

export interface ICreateUserResult {
  success: boolean;
  message?: string;
  data: any;
}

interface IUser {
  id: number;
  username: string;
  roleId: number;
  // roleName: string;
}

export interface IUserListResult {
  success: boolean;
  message?: string;
  data: IUser[];
}

export type IUpdateUserParams = Partial<ICreateUserParams>; // Partial 表示可选属性，即 ICreateUserParams 中的属性都可以省略，但是 roleId 必须有，因为它是必须的;

export interface IDeleteUserResult {
  success: boolean;
  message?: string;
}

export const login = (params: ILoginParams) => {
  return alova.Post<ILoginResult>("/user/login", params);
};

export const createUser = (params: ICreateUserParams) => {
  return alova.Post<ICreateUserResult>("/rbacuser", params);
};

export const getUserList = () => {
  return alova.Get<IUserListResult>("/rbacuser");
};

export const updateUser = (id: number, params: IUpdateUserParams) => {
  return alova.Patch<ICreateUserResult>(`/rbacuser/${id}`, params); // 这里的 /rbacuser/${id} 是后端的路由，不是前端的路由，前端的路由是 /userlis
};

export const deleteUser = (id: number) => {
  return alova.Delete<IDeleteUserResult>(`/rbacuser/${id}`);
};
